package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.MpsItemEntity;
import cn.com.huzo.model.entities.master.MpsItemUsageRuleEntity;
import cn.com.huzo.repository.master.MpsItemRepository;
import cn.com.huzo.repository.master.MpsItemUsageRuleRepository;
import cn.com.huzo.service.master.MpsItemManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主产品计划项目管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("MpsItemManager")
public class MpsItemManagerImpl extends GenericManagerImpl<MpsItemEntity, Long>
        implements MpsItemManager {

    @Resource(name = "MpsItemRepository")
    private MpsItemRepository _dao;

    @Resource(name = "MpsItemUsageRuleRepository")
    private MpsItemUsageRuleRepository _dao_rule;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<MpsItemEntity> fetchAll() throws Exception {
        return _dao.fetchAll();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<MpsItemEntity> fetchAllByUser(Long userId) throws Exception {
        return _dao.fetchAllByUser(userId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveLines(Map<String, ? extends List<MpsItemEntity>> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        if (criterion.get("insertList").size() != 0) {
            _dao.batchInsert(criterion.get("insertList"));
            _dao.resetLinesOrder();
        }
        if (criterion.get("updateList").size() != 0) {
            _dao.batchUpdate(criterion.get("updateList"));
        }

        _result.put("ItemList", _dao.fetchAll());
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        _dao.delete(MapUtils.getLong(criterion, "ItemId"));
        _dao.resetLinesOrder();

        Map<String, Object> _result = new HashMap();
        _result.put("ItemList", _dao.fetchAll());
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Integer updateLinesOrder(List<MpsItemEntity> itemList) throws Exception {
        return _dao.updateLinesOrder(itemList);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer grant(List<MpsItemUsageRuleEntity> itemList) throws Exception {
        // 删除当前授权用户相关的授权数据
        _dao_rule.revokeByUserId(itemList);
        // 重新授权
        return _dao_rule.batchInsert(itemList);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer revoke(Long userId) throws Exception {
        return _dao_rule.revoke(userId);
    }
}
